import { getAllCategory } from "../api/category";
import { topCategories } from "../api/constants";

export default {
  // 使用具有命名空间的 vuex 模块
  namespaced: true,
  state() {
    return {
      // 存储分类列表
      list: topCategories.map((name) => ({ name })),
      // 一级路由
      firstRouterData: "",
      // 二级路由
      secondRouterData: "",
      // 强制当第二次进入的时候才进行模板渲染
      num: 0,
    };
  },
  mutations: {
    // 存储分类数据
    sycGetAllCategory(state, payload) {
      // console.log(state);
      state.list = payload;
    },
    // 手动打开下拉导航
    open(state, payload) {
      let obj = state.list.find((item) => item.id === payload);
      obj.open = true;
    },
    // 手动关闭下拉导航
    close(state, payload) {
      let obj = state.list.find((item) => item.id === payload);
      obj.open = false;
    },
    // 强制措施
    forceImplement(state, payload) {
      let number = state.num;
      state.num = number + 1;
      console.log(state.num);
    },
  },
  actions: {
    async asyGetAllCategory(store, payload) {
      const data = await getAllCategory();
      // console.log(result);
      data.result.forEach((item) => {
        item.open = false;
      });

      store.commit("sycGetAllCategory", data.result);
    },
    asyForce(store, payload) {
      // console.log(11);
      store.commit("forceImplement", payload);
    },
  },
};
